import 'package:flutter/widgets.dart';
/// @description 自动无限旋转的组件
class XHAnimatedRotationBox extends StatefulWidget {
  const XHAnimatedRotationBox({
    super.key,
    this.duration = const Duration(seconds: 1),
    this.curve = Curves.linear,
    this.child,
  });

  final Widget? child;
  final Duration duration;
  final Curve curve;

  @override
  _XHAnimatedRotationBoxState createState() {
    return _XHAnimatedRotationBoxState();
  }
}

class _XHAnimatedRotationBoxState extends State<XHAnimatedRotationBox>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this, duration: widget.duration);
    _animationController.repeat();
  }

  @override
  Widget build(BuildContext context) {
    return RotationTransition(
      turns: CurvedAnimation(parent: _animationController, curve: widget.curve),
      child: widget.child,
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  void didUpdateWidget(XHAnimatedRotationBox oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.duration != widget.duration) {
      _animationController.duration = widget.duration;
      _animationController.stop();
      _animationController.repeat();
    }
  }
}
